package com.nattakute.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.nattakute.dao.ResourceDAO;
import com.nattakute.model.Resource;

@Repository("resourceDao")
public class ResourceDAOImpl implements ResourceDAO{

	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	
	public List<Resource> getResource() {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession().createQuery("from Resource r").list();
	}

	
	public Resource getResourceById(Integer id) {
		Resource resource = null;
		try {
			resource = (Resource)sessionFactory.getCurrentSession().load(Resource.class, id);
			return resource;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	
	public Boolean addResource(Resource resource) {
		try {
			sessionFactory.getCurrentSession().save(resource);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	
	public Boolean deleteResource(Resource resource) {
		try {
			sessionFactory.getCurrentSession().delete(resource);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	
	public Boolean updateResource(Resource resource) {
		try {
			sessionFactory.getCurrentSession().update(resource);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	
	@SuppressWarnings("unchecked")
	public List<Resource> getResourceInLab(String labId) {
		// TODO Auto-generated method stub
		Criteria resourceCriteria=sessionFactory.getCurrentSession().createCriteria(Resource.class);
		Criteria labCriteria=resourceCriteria.createCriteria("labID");
		List<Resource> resources=new ArrayList<Resource>();
		labCriteria.add(Restrictions.eq("labID", labId));
		
		resources=resourceCriteria.list();
		return resources;
		
	}

}
